package com.menhai.personmanagement.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
	/**
	 * 获得指定日期的前一天
	 * (参数为字符型yyyy-MM-dd)
	 * @param
	 * @return
	 * @throws Exception
	 */
	public static String getSpecifiedDayBefore(String specifiedDay) {
		// SimpleDateFormat simpleDateFormat = new
		// SimpleDateFormat("yyyy-MM-dd");
		Calendar c = Calendar.getInstance();
		Date date = null;
		try {
			date = new SimpleDateFormat("yyyy-MM-dd").parse(specifiedDay);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		c.setTime(date);
		int day = c.get(Calendar.DATE);
		c.set(Calendar.DATE, day - 1);

		String dayBefore = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
		return dayBefore;
	}
	
	/**
	 * 获得指定日期的前一天
	 * （参数为日期型）
	 * @param specifiedDay
	 * @return
	 */
	public static String getSpecifiedDayBefore(Date specifiedDay) {
		// SimpleDateFormat simpleDateFormat = new
		// SimpleDateFormat("yyyy-MM-dd");
		Calendar c = Calendar.getInstance();		
		c.setTime(specifiedDay);
		int day = c.get(Calendar.DATE);
		c.set(Calendar.DATE, day - 1);

		String dayBefore = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
		return dayBefore;
	}

	/**
	 * 获得指定日期的后一天
	 *  (参数为字符型yyyy-MM-dd)
	 * @param specifiedDay
	 * @return
	 */
	public static String getSpecifiedDayAfter(String specifiedDay) {
		Calendar c = Calendar.getInstance();
		Date date = null;
		try {
			date = new SimpleDateFormat("yyyy-MM-dd").parse(specifiedDay);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		c.setTime(date);
		int day = c.get(Calendar.DATE);
		c.set(Calendar.DATE, day + 1);

		String dayAfter = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
		return dayAfter;
	}
	
	/**
	 * 获得指定日期的后一天
	 * (参数为日期型)
	 * @param specifiedDay
	 * @return
	 */
	public static String getSpecifiedDayAfter(Date specifiedDay) {
		Calendar c = Calendar.getInstance();		
		c.setTime(specifiedDay);
		int day = c.get(Calendar.DATE);
		c.set(Calendar.DATE, day + 1);

		String dayAfter = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
		return dayAfter;
	}
	
	/**
	 * 时间转字符串
	 * @param date
	 * @param format
	 * @return
	 */
	public static String convertToString(Date date,String format){
		String result = "";
		SimpleDateFormat f=new SimpleDateFormat(format);
		result = f.format(date);		
		return result;
	}
	/**
	 * 字符串转时间
	 * @param
	 * @throws Exception
	 */
	
	public static Date convertToDate(String dateStr,String format){
		Date date=null;
		if(!"".equals(dateStr)){
			SimpleDateFormat sDateFormat=new SimpleDateFormat(format); 
			 try {
				date =sDateFormat.parse(dateStr);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		return date;
	}
	
	/**
	 * 字符串转DATE
	 * @param str
	 * @return
	 */
	public static Date strToDate(String format, String str){
		DateFormat df = new SimpleDateFormat(format); 
		try {
			Date date = df.parse(str);
			return date;
		} catch (ParseException e) {			
			e.printStackTrace();
		} 
		return null;
	}
	
	/**
	 * @return返回字符串格式 yyyy-MM-dd HH:mm:ss
	 */
	public static String getStringDate(Date currentTime) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateString = formatter.format(currentTime);
		return dateString;
	}

	/**
	 * 获得当前日期加减一定天数的时间
	 * @param val 加减的日期
	 * @return yyyy-MM-dd HH:mm
	 */
	public static String subOrAddDay(int val)
	{
		String add = null;
		try {
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			Date date = new Date();
			Calendar begin=Calendar.getInstance();
			begin.setTime(date);
			begin.add(Calendar.DAY_OF_MONTH,val);
			add = df.format(begin.getTime());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return add;
	}

	/**
	 * 获取当前日期加减时间返回yyyy-MM-dd HH:mm
	 * @param val 加减的时间
	 * @return
	 */
	public static String subOrAddTime(int val)
	{
		String add = null;
		try {
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			Date date = new Date();
			Calendar begin=Calendar.getInstance();
			begin.setTime(date);
			begin.add(Calendar.HOUR,val);
			add = df.format(begin.getTime());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return add;
	}

	public static String subOrAddMinut(int val)
	{
		String add = null;
		try {
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			Date date = new Date();
			Calendar begin=Calendar.getInstance();
			begin.setTime(date);
			begin.add(Calendar.MINUTE,val);
			add = df.format(begin.getTime());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return add;
	}
}
